package cn.gx.kevin.common.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

/**
 * @ClassName: ClearTempFileTimer
 * @Description: 定时清理 filePaths 存放的临时文件
 * @author hjwen
 * @date 2018年4月26日 下午11:08:04
 */
public class ClearTempFileTimer {

	static List<String> filePaths = new ArrayList<String>();
	public final Logger logger = org.slf4j.LoggerFactory.getLogger(ClearTempFileTimer.class);
	static Object lock = new Object();
	static boolean isStart = false;

	public static void putFilePath(String path) {
		if (isStart && StringUtils.isNotEmpty(path)) {
			synchronized (lock) {
				filePaths.add(path);
			}
		}
	}

	public void run() {
		isStart = true;
		synchronized (lock) {
			System.out.println(
					" ******************************** ClearTempFileTimer ClearTempFileTimer ClearTempFileTimer ClearTempFileTimer");
			try {
				for (String path : filePaths) {
					File f = new File(path);
					if (f.exists()) {
						f.delete();
						logger.debug("删除临时文件：" + path);
					}
				}
				filePaths.clear();
			} catch (Exception ex) {
				logger.error("{}", ex);
			}
		}
	}

}
